<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/14
 * Time: 17:39
 */
namespace App\Http\Controllers\Api\V1_8_8;

use App\Http\Requests\Api\V1_8_8\CreditCardUpdateBillStatusRequest;
use App\Http\Requests\Api\V1_8_8\CreditCardUpdateRequest;
use App\Services\CreditCardService;
use App\Services\RepaymentBillService;
use Illuminate\Http\Request;

class CreditCardController extends BaseController
{
    protected $creditCardService;

    public function __construct(CreditCardService $creditCardService)
    {
        parent::__construct();
        $this->creditCardService = $creditCardService;
    }

    public function creditCardList()
    {
        return $this->creditCardService->creditCardList($this->user['user_id']);
    }

    /**
     * 更新信用卡
     * @param CreditCardUpdateRequest $request
     * @param CreditCardService $creditCardService
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCreditCard(CreditCardUpdateRequest $request)
    {
        $repaymentMoney = empty($request->repayment_money) ? 0 : $request->repayment_money;
        return $this->creditCardService->updateCreditCard($this->user['user_id'], $request->id,
            $request->bill_day, $request->repayment_day, $request->credit_lines, $repaymentMoney);
    }

    /**
     * 删除或解绑信用卡
     * @param Request $request
     * @param CreditCardService $creditCardService
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroyCreditCard(Request $request)
    {
        if (empty($request->id)) {
            return $this->responseJson(422);
        }
        return $this->creditCardService->destroyCreditCard($request->id, $this->user['user_id']);
    }

    /**
     * 信用卡账单明细
     */
    public function creditCardBillDetail(Request $request)
    {
        if (empty($request->id)) {
            return $this->responseJson(422);
        }
        return $this->creditCardService->billDetail($this->user['user_id'], $request->id);
    }

    /**
     * 信用卡还款记录
     */
    public function creditCardRepaymentRecord(Request $request)
    {
        if (empty($request->id)) {
            return $this->responseJson(422);
        }
        return $this->creditCardService->repaymentRecord($this->user['user_id'], $request->id);
    }

    /**
     * 更新账单状态
     */
    public function updateBillStatus(CreditCardUpdateBillStatusRequest $creditCardUpdateBillStatusRequest)
    {
        return $this->creditCardService->updateBillStatus($creditCardUpdateBillStatusRequest->bill_id,
            $creditCardUpdateBillStatusRequest->bill_status);
    }

}